<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Order;
use App\Models\Option;
use App\Repositories\MemberRepositoryEloquent;

class SettlementCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:settlement';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单结算';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(MemberRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //到点时间结算
        $orders = Order::where('close_time','<=',time())->where('status','=',0)->get();
        if($orders){
            foreach($orders as $k=>$order){
                //获取订单行情
                $optionData = get_settle_option_data($order->symbol,date('Y-m-d H:i:s',$order->close_time));
                $this->settlement($order,$optionData);

            }

        }
        //止盈止损结算
        $options = Option::where('status','=',1)->get();
        foreach($options as $option){
            $optionData = get_option_data($option->code);
            $orders1 = Order::where('symbol','=',$option->code)->where(function($query) use ($optionData){
                $query->where('surplus_point', '<=', $optionData['last_price'])
                        ->orWhere('stop_loss_point', '>=',$optionData['last_price']);
            })->where('status','=',0)->where('direction','=',1)->get();
            foreach($orders1 as $k=>$order){
                //获取订单行情
                //$order->close_time = time();
                $this->settlement($order,$optionData);
            }

            $orders2 = Order::where('symbol','=',$option->code)->where(function($query) use ($optionData){
                $query->where('surplus_point', '>=', $optionData['last_price'])
                        ->orWhere('stop_loss_point', '<=',$optionData['last_price']);
            })->where('status','=',0)->where('direction','=',0)->get();
            foreach($orders2 as $k=>$order){
                //获取订单行情
                //$order->close_time = time();
                $this->settlement($order,$optionData);
            }

        }
    }

    protected function settlement($order,$optionData){
        if($order->direction==1){
            if($optionData['last_price'] >= $order->surplus_point){
                $optionData['last_price'] = $order->surplus_point;
            }

            if($optionData['last_price'] <= $order->stop_loss_point){
                $optionData['last_price'] = $order->stop_loss_point;
            }
        }else{
            if($optionData['last_price'] <= $order->surplus_point){
                $optionData['last_price'] = $order->surplus_point;
            }

            if($optionData['last_price'] >= $order->stop_loss_point){
                $optionData['last_price'] = $order->stop_loss_point;
            }
        }
        

        $spread = $optionData['last_price'] - $order->buy_point;
        $profit = $spread/$order->option->fluctuation*$order->option->fluctuation_price;

        if($order->direction==0){
            if($profit<0){//点数上涨
                $profit = abs($profit);
            }else{
                $profit = -abs($profit);
            }
        }

        $profit = $profit*$order->option->exchange_rate;
        //更改订单状态
        $order->close_point = $optionData['last_price'];
        $order->profit = $profit;
        $order->close_time = time();
        $order->status = 1;
        if($order->save()){
            $note = '解冻保证金('.$order->option->name.')['.$order->id.']';
            $result = $this->user->modifyBalance($order->user_id,$order->stop_loss_margin*$order->option->exchange_rate,9,$note);
            if($result['status']){
                $note = '订单结算('.$order->option->name.')['.$order->id.']';
                $result = $this->user->modifyBalance($order->user_id,$profit,10,$note);
            }
        }

        
    }
}
